﻿
Imports System.Data
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports EVOLTA.Promocion.Bl.Be
Imports EVOLTA.Promocion.Bl.Bc


Partial Class reportes_ReporteFacturaBBVA
    Inherits System.Web.UI.Page
    Implements System.Web.UI.ICallbackEventHandler

#Region "Instancias a las Capas de Entidad y Negocio"

    Dim objnegProyecto As New EVOLTA.Promocion.Bl.Bc.Rproyecto
    Dim objnegEdificio As New EVOLTA.Promocion.Bl.Bc.RedificioManz
    Dim objnegSeparacionVenta As New EVOLTA.Promocion.Bl.Bc.RseparacionVenta

#End Region

#Region "Variables Globales"
    Public dtbInformacion As New DataTable
#End Region

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Not Page.IsPostBack) And (Request.Params("__EVENTTARGET") Is Nothing) Then
            Me.Cargar_proyectos()
            ddlEdificio.Items.Add(New ListItem("-- SELECCIONE TORRE --", "0"))
            'Me.ddlProyecto.Attributes.Add("onchange", "if (this.options[this.selectedIndex].value == '%') {document.getElementById('ctl00_ContentPlaceHolder1_ddlEdificio').value=='-- SELECCIONAR --';} else {CallTheServer('divddlEdificio@' + this.options[this.selectedIndex].value)};return false;")
            ' Reinicializando controles
            Me.lblSubTitulo2.ForeColor = Drawing.Color.Red
            Me.lblSubTitulo3.Text = String.Empty
            Me.lnkBtnExportar.Enabled = False
            Dim cbReference As String
            cbReference = Page.ClientScript.GetCallbackEventReference(Me, "arg", "ReceiveServerData", "null", True)

            Dim callbackScript As String = ""
            callbackScript &= "function CallTheServer(arg)" & _
                              "{" & cbReference & "; }"
            Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "CallTheServer", callbackScript, True)
            'Me.cargarInformacion()
        Else
            If Me.ddlProyecto.SelectedItem.Value.ToString.Trim.Equals("%") Then
                Me.lblSubTitulo2.Text = String.Empty
                Me.lnkBtnExportar.Enabled = False
                Me.lblSubTitulo3.Text = "Para realizar una consulta debe de seleccionar un PROYECTO."
                Me.lblSubTitulo3.ForeColor = Drawing.Color.Red
            Else
                Me.lnkBtnExportar.Enabled = True
                Me.lblSubTitulo2.ForeColor = Drawing.Color.Black
                Me.lblSubTitulo3.ForeColor = Drawing.Color.Black

                Me.lblSubTitulo2.Text = "Información correspondiente al proyecto " & Me.ddlProyecto.SelectedItem.Text

                Dim strSubtitulo3 As String = String.Empty

                If Not Me.txtNroDocumento.Text.Trim.Equals(String.Empty) Then
                    strSubtitulo3 = "Filtrado por N° Documento del Cliente: " & Me.txtNroDocumento.Text.Trim()
                End If

                If Not Me.txtNombreCliente.Text.Trim.Equals(String.Empty) Then
                    If Not Me.txtNroDocumento.Text.Trim.Equals(String.Empty) Then
                        strSubtitulo3 = strSubtitulo3 & " | "
                    Else
                        strSubtitulo3 = "Filtrado por "
                    End If

                    strSubtitulo3 = strSubtitulo3 & "Nombre del Cliente: " & Me.txtNombreCliente.Text.Trim.ToUpper()
                End If

                Me.lblSubTitulo3.Text = strSubtitulo3
                Dim cbReference As String
                cbReference = Page.ClientScript.GetCallbackEventReference(Me, "arg", "ReceiveServerData", "null", True)
                Dim callbackScript As String = ""
                callbackScript &= "function CallTheServer(arg)" & _
                                  "{" & cbReference & "; }"
                Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "CallTheServer", callbackScript, True)
                Me.cargarInformacion()
            End If

            ViewState("vst_subtitulo_2") = Me.lblSubTitulo2.Text
            ViewState("vst_subtitulo_3") = Me.lblSubTitulo3.Text
        End If
    End Sub

    Private Sub Cargar_proyectos()
        Dim dtbProyectos As New DataTable
        dtbProyectos = objnegProyecto.negCargar_proyectos("EPAZCENPR")

        Me.ddlProyecto.DataSource = dtbProyectos
        Me.ddlProyecto.DataTextField = "descProyecto"
        Me.ddlProyecto.DataValueField = "Id"
        Me.ddlProyecto.DataBind()
    End Sub

    Private Sub cargarInformacion()
        Dim strProyectoId As Integer = Me.ddlProyecto.SelectedItem.Value.ToString()
        'Dim strEdificioId As Integer = Me.ddlEdificio.SelectedIndex.ToString()
        If (Me.hdfEdificio.Value.Trim = "%") Then
            Me.hdfEdificio.Value = "0"
        End If
        Dim strEdificioId As Integer = Me.hdfEdificio.Value.Trim
        Dim strNroDocumento As String = Me.txtNroDocumento.Text.Trim()
        Dim strNombreCliente As String = Me.txtNombreCliente.Text.Trim()

        Me.dtbInformacion = objnegSeparacionVenta.negCargar_reporte_BBVA(Me.Master.descEmpresa, strProyectoId, strEdificioId, strNroDocumento, strNombreCliente)

        Dim dvwInformacion As New DataView
        dvwInformacion = Me.dtbInformacion.DefaultView

        If Me.dtbInformacion.Rows.Count > 0 Then
            Session("ssn_Exportar") = dvwInformacion.ToTable

            Me.rptReporteGlobal.DataSource = Me.dtbInformacion
            Me.rptReporteGlobal.DataBind()
        End If
    End Sub

    Protected Sub lnkBtnExportar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkBtnExportar.Click
        Dim strNombreArchivo As String = "Reporte BBVA - Paz Centenario_" & Microsoft.VisualBasic.Right("0" & Date.Now.Day.ToString(), 2) & Microsoft.VisualBasic.Right("0" & Date.Now.Month.ToString(), 2) & Date.Now.Year.ToString()
        Dim strTituloHoja As String = "REPORTE BBVA - PAZ CENTENARIO"
        Dim strSubTituloHoja As String = ViewState("vst_subtitulo_2").ToString()

        Dim dtbInformacion As New DataTable

        dtbInformacion = Session("ssn_Exportar")

        If dtbInformacion.Rows.Count > 0 Then
            Dim objInfraestructura As New Infraestructura
            Dim strOpcionalCabecera As String = String.Empty

            Dim strSubtitulo3 As String = ViewState("vst_subtitulo_3").ToString()

            strOpcionalCabecera = "<tr><th colspan=""" & dtbInformacion.Columns.Count & """>" & strSubtitulo3 & "</th></tr>"
            objInfraestructura.Exportar_a_excel(strNombreArchivo, strTituloHoja, strSubTituloHoja, dtbInformacion, Nothing, strOpcionalCabecera)
        End If
    End Sub

    Protected Sub btnCancelar_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.ModalPopupExtender.Hide()
    End Sub
#Region "          CallBack                                 "
    Private _callbackArgument As String
    Public Function GetCallbackResult() As String Implements System.Web.UI.ICallbackEventHandler.GetCallbackResult
        Return _callbackArgument
    End Function
    Public Sub RaiseCallbackEvent(ByVal eventArgument As String) Implements System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent
        Try
            If eventArgument <> String.Empty Then
                Dim varParams() As String = eventArgument.Split("@")
                Dim sr As New System.IO.StringWriter()
                Dim htm As New HtmlTextWriter(sr)
                _callbackArgument = ""
                Select Case varParams(0)
                    Case "divddlEdificio"
                        Me.ddlEdificio.DataSource = objnegEdificio.negCargar_Edificio(Me.Master.descEmpresa, varParams(1).ToString)
                        Me.ddlEdificio.DataTextField = "descTorre"
                        Me.ddlEdificio.DataValueField = "id"
                        Me.ddlEdificio.DataBind()
                        'Me.ddlEdificio.Value = varParams(1).ToString
                        'Me.ddlEdificio.Attributes.Remove("disabled")
                        Me.ddlEdificio.RenderControl(htm)
                        htm.Flush()
                        _callbackArgument = varParams(0).ToString + "|" + sr.ToString()
                End Select
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
#End Region

End Class
